<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Cache;
//require_once './sdk/WxpayAPI/lib/WxSend.Api.php';
class AutomaticRfuse extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('AutomaticRfuse')
            ->setDescription('the AutomaticRfuse command');
    }

    protected function execute(Input $input, Output $output)
    {
        try {
//            // 指令输出
////    	$output->writeln('automaticdelivery');
//            // 获取自动收货时间
            $time = Db::table('eb_config')->where('name', 'automatic_refuse')->value('value');
            if ($time === null) exit;
            $times = $time * 60;
            // 自动收货(系统评价)
//            $map = '`a.is_pay` = 0';
            $page = 1;
            $limit = 1000;
            while (true) {
                $ids = Db::table('eb_order')->where('status', 0)->where('is_pay', 0)->field('id,order_no,create_time')->page($page, $limit)->select()->toArray();
                if (!$ids) break;
                foreach ($ids as $key => $value) {
                    $a = $value['create_time'] + $times;//超时订单
                    if (time() >= $a) {
                        file_put_contents('/tmp/refuse_stock1.txt', json_encode($value['order_no']), FILE_APPEND);
                        $rtn = Db::table('eb_order')->where('order_no', $value['order_no'])->update(['status' => -4]);
                        file_put_contents('/tmp/refuse_stock2.txt', $rtn, FILE_APPEND);
                        if ($rtn==0) break;
                        $cart_list = Db::table('eb_order_detail')->where('order_id', $value['id'])->field('store_id,meat_text,veg_text,goods_id,goods_num')->select()->toArray();
                        file_put_contents('/tmp/refuse_stock3.txt', json_encode($cart_list), FILE_APPEND);
                        foreach ($cart_list as $key1 => $value1) {
                            if(!empty($value1['meat_text'])){
                                $meat_text = explode(',', $value1['meat_text']);
                                $veg_text = explode(',', $value1['veg_text']);
                                foreach ($meat_text as $v) {//减少荤菜库存
                                    $red = Db::execute("update eb_goods_conf  set `stock`=`stock`+{$value1['goods_num']} where store_id={$value1['store_id']} and stock>=1 and `name` ='{$v}'");
                                    if ($red == false) break;
                                }
                                foreach ($veg_text as $v) {//减少素菜库存
                                    $red = Db::execute("update eb_goods_conf  set `stock`=`stock`+{$value1['goods_num']} where store_id={$value1['store_id'] } and stock>=1 and `name` ='{$v}'");
                                    if ($red == false) break;
                                }
                            }
                            $res = Db::table('eb_goods')->where('id', $value1['goods_id'])->inc('stock', $value1['goods_num'])->update();
                            $stock = Cache::store('redis')->handler()->hGet('dxsd-goods-' . $value1['goods_id'], 'stock');
                            $editRtn = Cache::store('redis')->handler()->hSet('dxsd-goods-' . $value1['goods_id'], 'stock',$stock+$value1['goods_num']);
                            file_put_contents('/tmp/refuse_stock4.txt', json_encode($editRtn), FILE_APPEND);
                            if (!$res) break;
                        }
                        $page++;
                    } else {
                        break;
                    }
                }
                //if (!$rtn) break;
                $page++;
            }
        } catch (\Exception $e) {
            file_put_contents('/tmp/refuse_error.txt',$e->getMessage(), FILE_APPEND);
        }
    }
}
